﻿namespace LxCFD
{
    public class QueueTool
    {
        //readonly int EMPTY_FLAG = -1;
        Queue[] Queues;
        One[] Ones;
        public void Create(int queueLength, int oneLength)
        {
            Queues = new Queue[queueLength];
            Ones = new One[oneLength];
            Reset();
        }
        public void Reset()
        {
            for (int i1 = 0; i1 < Queues.Length; ++i1)
            {
                ref Queue q = ref Queues[i1];
                q.Length = 0;
            }
        }
        public void AddOneToQueue(int queueID, int oneID)
        {
            ref Queue q = ref Queues[queueID];
            if (q.Length == 0)
            {
                q.LastOne = oneID;
            }
            else
            {
                Ones[oneID].PrevOneID = q.LastOne;
                q.LastOne = oneID;
            }
            ++q.Length;
        }
        public void ConvertQueueToArray(out int[] array, int queueID)
        {
            ref Queue q = ref Queues[queueID];
            array = new int[q.Length];
            int prevOne = q.LastOne;
            for (int pos = q.Length - 1; pos >= 0; --pos)
            {
                array[pos] = prevOne;
                prevOne = Ones[prevOne].PrevOneID;
            }
        }
        struct Queue
        {
            public int Length;
            public int LastOne;
        }
        struct One
        {
            public int PrevOneID;
        }
    }
}
